function encode(val) {
	var obj = document.getElementById(val);
	var unencoded = trim(obj.value);
	str = encodeURIComponent(unencoded);
	return str;
}
function decode(val) {
	var obj = document.getElementById(val);
	var encoded = obj.value;
	return decodeURIComponent(encoded.replace(/\+/g,  " "));
}

function attrFocus() {

//alert(document.f1.classX.value + " " + document.f1.classY.value + " " + document.f1.classZ.value)
	if (typeof(document.f1.classX) != "undefined" && typeof(document.f1.classY) != "undefined" && document.f1.classX.value != "" && document.f1.classY.value == "") {
		attrRestrX();
		attrRestrY();
		attrRestrZ();
		//alert("X");
	} else if (typeof(document.f1.classY) != "undefined" && typeof(document.f1.classZ) != "undefined" && document.f1.classY.value != "" && document.f1.classZ.value == "") {
		attrRestrY();
		attrRestrZ();
		//alert("X and Y");
	} else if (typeof(document.f1.classZ) != "undefined" && document.f1.classZ.value != "") {
		attrRestrZ();
		//alert("X, Y and Z");
	}

}

function escapeVal(s) {
  var retStr = escape(s);
  // replace all unicode symbols
  var ix = retStr.indexOf("%");
  while(ix >= 0){
    if(ix+1 > retStr.length){
      retStr = retStr.substring(0, ix) + "\\\\";
    }else{
      retStr = retStr.substring(0, ix) + "\\\\" + retStr.substring(ix+1);
    }
    ix = retStr.indexOf("%", ix+2);
  }

  // replace all spaces back
  ix = retStr.indexOf("\\\\20");
  while(ix >= 0){
    if(ix+1 > retStr.length){
      retStr = retStr.substring(0, ix) + "%20";
    }else{
      retStr = retStr.substring(0, ix) + "%20" + retStr.substring(ix+4);
    }
    ix = retStr.indexOf("\\\\20", ix+3);
  }

  return retStr;
}

//--------------------------
// form elements generation and management

varChoice0 = "X";


function literalExpr( fieldName  ) {

    document.write("<select class=\"select\" name=\"" + fieldName + "\" onChange=\"literalExprOnChange(" + fieldName + ");\">");
    document.write("<option value=\"is unknown\">is unknown</option>");
    document.write("<option value=\"is exactly =\">is exactly =</option>");
    document.write("<option value=\"starts with\">starts with</option>");
    document.write("<option value=\"ends with\">ends with</option>");
    document.write("<option value=\"contains\">contains</option>");
    document.write("<option value=\"is &lt; (before)\"> is &lt; (before)</option>");
    document.write("<option value=\"is &gt; (after)\"> is &gt; (after)</option>");
    document.write("<option value=\"equals ignore case\">equals ignore case</option>");
    document.write("</select> ");

} // literalExpr

function literalExprOnChange(fieldName) {

	if (document.f1.nameX.selectedIndex != 0) {
		document.getElementById("xtext").style.visibility = "visible";
		document.getElementById("xtext").style.height = "60px";
		} else {
		document.getElementById("xtext").style.visibility = "hidden";
		document.getElementById("xtext").style.height = "1px";
		}

	if (document.f1.nameY.selectedIndex != 0) {
		document.getElementById("ytext").style.visibility = "visible";
		document.getElementById("ytext").style.height = "60px";
		} else {
		document.getElementById("ytext").style.visibility = "hidden";
		document.getElementById("ytext").style.height = "1px";
		}

	if (document.f1.nameZ.selectedIndex != 0) {
		document.getElementById("ztext").style.visibility = "visible";
		document.getElementById("ztext").style.height = "60px";
		} else {
		document.getElementById("ztext").style.visibility = "hidden";
		document.getElementById("ztext").style.height = "1px";
		}
		
	if (document.f1.attrExpr1.selectedIndex != 0) {
		document.getElementById("attrEx1").style.visibility = "visible";
		document.getElementById("attrEx1").style.height = "60px";
		} else {
		document.getElementById("attrEx1").style.visibility = "hidden";
		document.getElementById("attrEx1").style.height = "1px";
		}

	if (document.f1.attrExpr2.selectedIndex != 0) {
		document.getElementById("attrEx2").style.visibility = "visible";
		document.getElementById("attrEx2").style.height = "60px";
		} else {
		document.getElementById("attrEx2").style.visibility = "hidden";
		document.getElementById("attrEx2").style.height = "1px";
		}

	if (document.f1.attrExpr3.selectedIndex != 0) {
		document.getElementById("attrEx3").style.visibility = "visible";
		document.getElementById("attrEx3").style.height = "60px";
		} else {
		document.getElementById("attrEx3").style.visibility = "hidden";
		document.getElementById("attrEx3").style.height = "1px";
		}

}

//-----------------------------
// class Select/combobox

function classSel( fieldName ) {
    document.write( "<select style=\"width:180px\" class=\"select\" name=\"" + fieldName + "\"");
    document.write(" onChange=\"{classSelOnChange(" + fieldName + ");attrFocus();propcheck();}\">");
    document.write("</select>");
    theSel = document.f1.elements[fieldName];
    theSel.dependent = new Array();
} // classSel

//-----------------------------
// class Select/combobox for Timelines

function classSelTimelines( fieldName ) {
    document.write( "<select style=\"width:180px\" class=\"select\" name=\"" + fieldName + "\"");
    document.write(" onChange=\"{classSelOnChange(" + fieldName + ");}\">");
    document.write("</select>");
    theSel = document.f1.elements[fieldName];
    theSel.dependent = new Array();
} // classSelTimelines

function checkClasses () {
	if ((document.f1.refSel.selectedIndex == 1) && (document.f1.classY.value == "")) {
		//alert("Pattern for Y class is empty."); 
		document.getElementById("errMsg").innerHTML = "* Please, select a pattern for Y first.";
		setTimeout('document.getElementById("errMsg").innerHTML = ""', 3000);
		document.f1.refSel.options[0].selected = true;
	}
		if ((document.f1.refSel.selectedIndex == 2) && (document.f1.classZ.value == "")) {
		//alert("Pattern for Z class is empty."); 
		document.getElementById('errMsg').innerHTML = "* Please, select a pattern for Z first.";
		setTimeout('document.getElementById("errMsg").innerHTML = ""', 3000);
		document.f1.refSel.options[0].selected = true;
	}
		if ((document.f1.refSel.selectedIndex == 3) && ((document.f1.classY.value == "") && (document.f1.classZ.value == ""))) {
		//alert("Pattern for Y or Z classes is empty."); 
		document.getElementById('errMsg').innerHTML = "* Please, select a patterns for Y and Z first.";
		setTimeout('document.getElementById("errMsg").innerHTML = ""', 3000);
		document.f1.refSel.options[0].selected = true;
	}
		if ((document.f1.refSel.selectedIndex == 3) && ((document.f1.classY.value != "") && (document.f1.classZ.value == ""))) {
		//alert("Pattern for Y or Z classes is empty."); 
		document.getElementById('errMsg').innerHTML = "* Please, select a patterns for Z first.";
		setTimeout('document.getElementById("errMsg").innerHTML = ""', 3000);
		document.f1.refSel.options[0].selected = true;
	}
}

function isUrl(s) {
    // URIs like urn:lsid:ontotext.com:kim:iextraction:GeneOntologyTerm should be allowed
    return true; 
    
	var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	var str = document.f1.ADDITIONAL_TYPE_RESTRICTION.value;
	//return regexp.test(s);
	if (regexp.test(s) || str == "") {
		return true;
	} else {
		//alert("You've entered invalid URI. It has to start with \"http://\" and must be a valid address. \nExample: http://proton.semanticweb.org/2005/04/protons#Entity");
		//if (str.indexOf("http://") == -1) {
		//	document.f1.ADDITIONAL_TYPE_RESTRICTION.value = "http://" + document.f1.ADDITIONAL_TYPE_RESTRICTION.value;
		//} else if (str.indexOf("http://") == 0) {
		//	document.f1.ADDITIONAL_TYPE_RESTRICTION.value = document.f1.ADDITIONAL_TYPE_RESTRICTION.value;
		//} else {
		//	document.f1.ADDITIONAL_TYPE_RESTRICTION.value = "http://";
		//}
		//document.getElementById("invalidURI").innerHTML = "* Invalid URI. It has to start with \"http://\"";
		document.f1.ADDITIONAL_TYPE_RESTRICTION.value = "http://"+str;
		//setTimeout('document.getElementById("invalidURI").innerHTML = ""', 4000);
		return true; 
	}
}

function initIndepClassSel( select ) {
    while (select.options.length > 1)
        select.options[1] = null;

    childrenAsOptions(select, cTop.children, "");

//  history.go(0);
} // initIndepClassSel


function classSelUpdate( select, propName ) {

    while (select.options.length > 0)
        select.options[0] = null;

        if (propName == "")
                return;

    property = eval("p" + propName);
    if (property != null) {
        childrenAsOptions(select, property.range, "");
    }

//  history.go(0);
} // classSelUpdate

function childrenAsOptions(sel, children, offset) {

  for (var i=0; i < children.length; i++) {
    var child = children[i];
    var newLabel = offset + child.label;
    newOpt = new Option(newLabel, child.name, false, false);
    var isAvailable = false;
    for(var j=0; j<sel.options.length; j++){
      if(sel.options[j].value == child.name){
        if(sel.options[j].text.length < newLabel.length){
          sel.options[j] = null;
    sel.options[sel.options.length] = newOpt;


        }
        isAvailable = true;
      }
    } // for
    if(!isAvailable){
        sel.options[sel.options.length] = newOpt;

        if (child.children.length > 0) {
            childrenAsOptions(sel, child.children, offset + "--")
        }
    }
  } // for
} // childrenAsOptions

function classSelOnChange(field) {
	var dep = field.dependent;
	if(dep == null) return;
    for (var i=0; i < dep.length; i++) {
        var propSel = eval( "document.f1." + field.dependent[i]);
        var theProp = trimNS(field.value);
        propSelUpdate(propSel, theProp);
        propSelOnChange(propSel);
		
		
    } // for
} // classSelOnChange
//-----------------------------
// property Select/combobox

function changeObjectVisibility(objectId, newVisibility) {
    var styleObject = getStyleObject(objectId, document);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	return false;
    }
} 


function propertySel( fieldName, dependentClassVar ) {

    document.write("<select style=\"width:180px\" class=\"select\" name=\"" + fieldName +
                    "\" onChange=\"{propSelOnChange(" + fieldName + "); checkClasses();attrFocus();propcheck();}\">");
    document.write("<option value=\"\"></option>");
    document.write("</select>");

    theSel = document.f1.elements[fieldName];
    theSel.dependent = dependentClassVar;
} // propertySel

function propcheck() {
	if (document.f1.prop1.selectedIndex	!= 0) {
		document.getElementById("ydiv").style.visibility = "visible";
		document.getElementById("ydiv").style.height = "60px";
	} else {
		//alert("document.f1.prop1.selectedIndex = 0");
		document.getElementById("ydiv").style.visibility = "hidden";
		document.getElementById("zdiv").style.visibility = "hidden";
		document.getElementById("ydiv").style.height = "1px";
		document.getElementById("zdiv").style.height = "1px";
		document.f1.nameY.selectedIndex = 0;
		document.f1.nameZ.selectedIndex = 0;
		document.f1.nameYText.value = "";
		document.f1.nameZText.value = "";
		document.getElementById("ytext").style.visibility = "hidden";
		document.getElementById("ytext").style.height = "1px";
	}
	if (document.f1.prop2.selectedIndex	!= 0) {
		document.getElementById("zdiv").style.visibility = "visible";
		document.getElementById("zdiv").style.height = "40px";
	} else {
		//alert("document.f1.prop2.selectedIndex = 0");
		document.getElementById("zdiv").style.visibility = "hidden";
		document.getElementById("zdiv").style.height = "1px";
		//document.f1.nameY.selectedIndex = 0;
		document.f1.nameZ.selectedIndex = 0;
		//document.f1.nameYText.value = "";
		document.f1.nameZText.value = "";
		document.getElementById("ztext").style.visibility = "hidden";
		document.getElementById("ztext").style.height = "1px";
	}

	if (document.f1.attr1.length > 1) {
		document.getElementById("attributeExpr1").style.visibility = "visible";
		document.getElementById("attributeExpr1").style.height = "30px";
		document.getElementById("noAttr").innerHTML = "";
	} else {
		document.getElementById("attributeExpr1").style.visibility = "hidden";
		document.getElementById("attributeExpr1").style.height = "1px";
		document.getElementById("attrEx1").style.visibility = "hidden";
		document.getElementById("attrEx1").style.height = "1px";
		document.f1.attrExpr1.selectedIndex = 0;
	}
	if (document.f1.attr2.length > 1) {
		document.getElementById("attributeExpr2").style.visibility = "visible";
		document.getElementById("attributeExpr2").style.height = "30px";
		document.getElementById("noAttr").innerHTML = "";
	} else {
		document.getElementById("attributeExpr2").style.visibility = "hidden";
		document.getElementById("attributeExpr2").style.height = "1px";
		document.getElementById("attrEx2").style.visibility = "hidden";
		document.getElementById("attrEx2").style.height = "1px";
		document.f1.attrExpr2.selectedIndex = 0;
	}
	if (document.f1.attr3.length > 1) {
		document.getElementById("attributeExpr3").style.visibility = "visible";
		document.getElementById("attributeExpr3").style.height = "30px";
		document.getElementById("noAttr").innerHTML = "";
	} else {
		document.getElementById("attributeExpr3").style.visibility = "hidden";
		document.getElementById("attributeExpr3").style.height = "1px";
		document.getElementById("attrEx3").style.visibility = "hidden";
		document.getElementById("attrEx3").style.height = "1px";
		document.f1.attrExpr3.selectedIndex = 0;
	}
	if (document.f1.attr1.length <= 1 && document.f1.attr2.length <= 1 && document.f1.attr3.length <= 1) {
		document.getElementById("noAttr").innerHTML = "No attributes available.";	
	}
}

function propSelOnChange(field) {
    var theClassSel = eval( "document.f1." + field.dependent);
    if (theClassSel == null)
                return;

    var theClass = trimNS(field.value);
    classSelUpdate(theClassSel, theClass);
    classSelOnChange(theClassSel);
} // propSelOnChange


function propSelUpdate( select, className ) {
    while (select.options.length > 0)
        select.options[0] = null;
    select.options[0] = new Option("---", "", false, false);
    if (className == "")
                return;
    theClass = eval("c" + className);
    if (theClass != null) {
                if (select.name.charAt(0) == 'p') {
				
                        childrenAsOptions(select, theClass.props, "");
				} else {
                        childrenAsOptions(select, theClass.attribs, "");
				}
	}
	

//  history.go(0);
} // propSelUpdate

//-----------------------------
// variable Select/combobox

function varSel(varSelName, vars, dependent) {
    document.write("<select class=\"edit1\" name=\"" + varSelName +
                    "\" onfocus=\"{varSelOnChange(" + varSelName + ");}\">");
//document.write("<option value='a'>---</option>");
    for (var i=0; i<vars.length; i++) {
        document.write("<option value=\"" + vars[i] + "\">" + vars[i] + "</option>");
    }
    document.write("</select>");
//alert(document.f1.varSel5.value);
    theSel = eval("document.f1." + varSelName);
    theSel.dependent = dependent;
    theSel.prevVal = theSel.value;

    newClassSel = eval("document.f1.class" + theSel.value);
    push(newClassSel.dependent,theSel.dependent);
	//alert(dependent);
	//alert(document.f1.classX.value+"\n"+document.f1.classY.value+"\n"+document.f1.classZ.value+"\n");
} // varSel

function varSelOnChange(aVarSel) {
	
    prevClassSel = eval("document.f1.class" + aVarSel.prevVal);
	//alert(aVarSel.prevVal);
    for (var i=0; i < prevClassSel.dependent.length; i++) {
        if (prevClassSel.dependent[i] == aVarSel.dependent) {
            prevClassSel.dependent = splice(prevClassSel.dependent, i, 1);
//            prevClassSel.dependent.splice(i,1);
            break;
        }
    } // for

    newClassSel = eval("document.f1.class" + aVarSel.value);
    push(newClassSel.dependent, aVarSel.dependent);

    var theClass = trimNS(newClassSel.value);
    propSelUpdate(eval( "document.f1." + aVarSel.dependent), theClass);

    aVarSel.preVal = aVarSel.value;	

//attrRestrX();
//attrRestrY();
//attrRestrZ();
	//alert("aVarSel=" + aVarSel.name + "\naVarSel.prevVal=" + aVarSel.prevVal + "\naVarSel.dependent=" + aVarSel.dependent + "\nprevClassSel=" + prevClassSel.name + "\nnewClassSel=" + newClassSel.name + "\nprevClassSel.dependent=" + prevClassSel.dependent + "\nnewClassSel.dependent=" + newClassSel.dependent + "\ntheClass=" + theClass + "\nprevClassSel.dependent[0]=" + prevClassSel.dependent[0]);
} // varSelOnChange

function attrRestrX() {
    newClassSel = eval("document.f1.classX");
    var theClass = trimNS(newClassSel.value);
    propSelUpdate(eval("document.f1.attr1"), theClass);
}
function attrRestrY() {
    newClassSel = eval("document.f1.classY");
    var theClass = trimNS(newClassSel.value);
    propSelUpdate(eval("document.f1.attr2"), theClass);
}
function attrRestrZ() {
    newClassSel = eval("document.f1.classZ");
    var theClass = trimNS(newClassSel.value);
    propSelUpdate(eval("document.f1.attr3"), theClass);
}

// attribute Select/combobox

function attributeSel( fieldName ) {

    document.write("<select style=\"width:180px\" class=\"select\" name=\"" + fieldName + "\">");
    document.write("<option value=\"\"></option>");
    document.write("</select>");

} // attributeSel


//--------------------------------------------
// UI styles and behaviour

// function for edit boxes
function txtNormal(obj) {
        obj.className="edit2";
}
function txtFocus(obj) {
  obj.className="edit2";
  obj.select();
}

// functions for buttons
function btnNormal(obj) {
  obj.className="button1";
}
function btnOnFocus(obj) {
  obj.className="button2";
}

//--------------------------------------------
// select an item in a combobox
function selComboItem(comboControl, matchValue){
	if(comboControl != null){
		for(var i=0; i<comboControl.options.length; i++){
			if(comboControl.options[i].value == matchValue){
				comboControl.selectedIndex = i;
				return;
			}
		}
	}
}

//--------------------------------------------
// trims the NS
function trimNS(value){
	 //alert("Deprecated function trimNS called with param " + value);
     var retStr = value.toString();
//	 var ix = retStr.lastIndexOf('#');
//	 if (ix < 0)
//		 ix = retStr.lastIndexOf(':');
//     if(ix>-1){
//      retStr = retStr.substring(ix+1,retStr.length);
//    }
//    return retStr;

	
	retStr = retStr.replace(new RegExp("[^0-9a-zA-Z_]", "g"), "_");
	return retStr;
}

